package com.doctor.ui.setting;

import java.awt.font.NumericShaper;
import java.io.File;
import java.util.List;

import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.doctor.comm.ConstConfig;
import com.doctor.comm.FileHelper;
import com.doctor.comm.ImageHelper;
import com.doctor.comm.LoadingTip;
import com.doctor.comm.MyApplication;
import com.doctor.comm.ImageHelper.TakePictureCallback;
import com.doctor.database.DES;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseActivity;
import com.doctor.ui.R;

public class UserInfoActivity extends BaseActivity{

	/**
	 * 设置个人信息
	 */
	private EditText name, hospital;
	private ImageView photo;
	private TextView uploadBtn,takePhotoBtn;
	private LinearLayout saveBtn,cancelBtn;
	private Uri imageUri;
	private Bitmap bitmap;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_info_activity);
		
		initiaView();
		getData();
	}
	
	@Override
	protected void initiaView() {
		// TODO Auto-generated method stub
		name=(EditText) findViewById(R.id.name);
		hospital=(EditText) findViewById(R.id.hospital);
		photo=(ImageView) findViewById(R.id.photo);
		
		Click click=new Click();
		uploadBtn=(TextView) findViewById(R.id.uploadBtn);
		uploadBtn.setOnClickListener(click);
		
		takePhotoBtn=(TextView) findViewById(R.id.takePhotoBtn);
		takePhotoBtn.setOnClickListener(click);
		
		saveBtn=(LinearLayout) findViewById(R.id.saveBtn);
		saveBtn.setOnClickListener(click);
		
		cancelBtn=(LinearLayout) findViewById(R.id.cancelBtn);
		cancelBtn.setOnClickListener(click);
	}
	
	@Override
	protected void getData() {
		// TODO Auto-generated method stub
		List<String> info = DbOperator.getInstance(UserInfoActivity.this).selectUserLocalInfo();
		if (info.size() > 0) {
			name.setText(info.get(0));
			hospital.setText(info.get(2));
			String photoPath=info.get(3);
			if (photoPath!=null && !"".equals(photoPath)) {
				imageUri = Uri.fromFile(new File(photoPath));
				Bitmap bitmap = ImageHelper.getBitmapFromPath(this, photoPath);
				if (bitmap != null) {
					photo.setImageBitmap(bitmap);
				}
			}
		}
	}
	
	private class Click implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v==uploadBtn) {
				ImageHelper.selectPicture(UserInfoActivity.this, ConstConfig.CHOOSE_PICTURE,null);
			} else if (v==takePhotoBtn) {
				ImageHelper.selectPicture(UserInfoActivity.this, ConstConfig.TAKE_PICTURE,new TakePictureCallback() {					
					@Override
					public void onGetImageName(Uri imageUri) {
						// TODO Auto-generated method stub
						UserInfoActivity.this.imageUri=imageUri;
					}
				});
			} else if (v == saveBtn) {
				ContentValues values=new ContentValues();
				String name_txt=name.getText().toString().trim();
				String hospital_txt=hospital.getText().toString().trim();
				values.put("_localName", name_txt);
				values.put("_localHospital", hospital_txt);
				if (imageUri!=null) {
					values.put("_localPhoto", FileHelper.uri2Path(UserInfoActivity.this, imageUri));
				} else {
//					LoadingTip.showToast(UserInfoActivity.this, "请设置新头像！");
//					return;
					values.put("_localPhoto", "");
				}
				
				String id = null;
				try {
					id=DES.encryptDES(String.valueOf(MyApplication.getInstance().getUserId()),DES.KEY);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				int row=DbOperator.getInstance(UserInfoActivity.this).updateData(ConstConfig.USER_TABLE, values, id);
				if (row>0) {
					LoadingTip.showToast(UserInfoActivity.this, "保存成功！");
					MyApplication.getInstance().freshUser();
					UserInfoActivity.this.finish();
				} else {
					LoadingTip.showToast(UserInfoActivity.this, "保存失败！");
				}
				
			} else if (v==cancelBtn) {
				UserInfoActivity.this.finish();
			}
		}
	}
	
	//选择图片或拍照完成后返回图片路径
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			Uri imageUri = null;	
			switch (requestCode) {
			case ConstConfig.CHOOSE_PICTURE:
				if (data!=null) {
					imageUri=data.getData();
				}
				break;
				
			case ConstConfig.TAKE_PICTURE:
				imageUri=UserInfoActivity.this.imageUri;
				UserInfoActivity.this.imageUri=null;
				break;
				
			default:
				break;
			}
			if (imageUri!=null) {
				UserInfoActivity.this.imageUri=imageUri;
				if (bitmap != null) {
					bitmap.recycle();
					bitmap = null;
					System.gc();
				}
				String path = FileHelper.uri2Path(UserInfoActivity.this, imageUri);
				bitmap = ImageHelper.getBitmapFromPath(UserInfoActivity.this, path);
				if (bitmap != null) {
					photo.setImageBitmap(bitmap);
				}
			}
		} 
	}	
	
	
}
